package org.liuyuantao.lucene.parser;

import java.text.SimpleDateFormat;
import java.util.regex.Pattern;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query;

public class CustomParserExample extends QueryParser {

    public CustomParserExample(String f, Analyzer a) {
        super(f, a);
    }

    /**
     * 关闭通配符查询并提示相应信息
     *
     * @param field
     * @param termStr
     * @return
     * @throws ParseException
     */
    @Override
    protected Query getWildcardQuery(String field,
                                     String termStr) throws ParseException {
        throw new ParseException("由于性能原因，已经禁用了通配符查询，请输入更精确的信息进行查询");
    }

    /**
     * 关闭模糊查询并提示相应信息
     *
     * @param field
     * @param termStr
     * @param minSimilarity
     * @return
     * @throws ParseException
     */
    @Override
    protected Query getFuzzyQuery(String field,
                                  String termStr, float minSimilarity) throws ParseException {
        throw new ParseException("由于性能原因，已经禁用了模糊查询，请输入更精确的信息进行查询");
    }

    /**
     * 自定义区间查询
     *
     * @param field
     * @param part1
     * @param part2
     * @param startInclusive
     * @param endInclusive
     * @return
     * @throws ParseException
     */
    @Override
    protected Query getRangeQuery(String field,
                                  String part1, String part2, boolean startInclusive, boolean endInclusive)
            throws ParseException {

        if (field.equals("size")) {
            //自定义文件大小的区间查询
            return NumericRangeQuery.newIntRange(field, Integer.parseInt(part1), Integer.parseInt(part2), startInclusive, endInclusive);
        } else if (field.equals("date")) {
            //自定义文件日期的区间查询
            String dateType = "yyyy-MM-dd";
            Pattern pattern = Pattern.compile("\\d{4}-\\d{2}-\\d{2}");
            if (pattern.matcher(part1).matches() && pattern.matcher(part2).matches()) {
                SimpleDateFormat sdf = new SimpleDateFormat(dateType);
                try {
                    long start = sdf.parse(part1).getTime();
                    long end = sdf.parse(part2).getTime();
                    return NumericRangeQuery.newLongRange(field, start, end, startInclusive, endInclusive);
                } catch (java.text.ParseException e) {
                    e.printStackTrace();
                }
            } else {
                throw new ParseException("要检索的日期格式不正确，请使用" + dateType + "这种格式");
            }
        }
        return super.newRangeQuery(field, part1, part2, startInclusive, endInclusive);
    }
}
